from sqlalchemy import Column, BigInteger, SmallInteger, Date, DateTime, Index
from app.config.database import Base
from datetime import datetime, date
from pydantic import BaseModel, ConfigDict
from typing import Optional

class ClassStudent(Base):
    __tablename__ = "edu_class_student"
    
    id = Column(BigInteger, primary_key=True, autoincrement=True, comment="关联ID")
    class_id = Column(BigInteger, nullable=False, comment="班级ID")
    student_id = Column(BigInteger, nullable=False, comment="学生ID")
    join_date = Column(Date, nullable=False, comment="加入日期")
    leave_date = Column(Date, comment="离开日期")
    status = Column(SmallInteger, nullable=False, default=1, comment="状态：0-已离开 1-在读")
    create_time = Column(DateTime, default=datetime.utcnow, nullable=False, comment="创建时间")
    update_time = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False, comment="更新时间")
    
    # 索引
    __table_args__ = (
        Index('uk_class_student', 'class_id', 'student_id'),
        Index('idx_class_id', 'class_id'),
        Index('idx_student_id', 'student_id')
    )

class ClassStudentInDB(BaseModel):
    id: int
    class_id: int
    student_id: int
    join_date: date
    leave_date: Optional[date] = None
    status: int = 1
    create_time: Optional[datetime] = None
    update_time: Optional[datetime] = None

    model_config = ConfigDict(from_attributes=True)